﻿using System;

namespace ConsoleApp2
{
    class Program
    {

        static int MAX_MOVES = 9;
        static int foot = 0;
        //15代表棋子一，88代表棋子二
        public static void Main(string[] args)
        {
            int i;
            int n = 0;
            int[,] borad = { { 7, 8, 9 }, { 4, 5, 6 }, { 1, 2, 3 } };
            PrintBoard(borad);
            for (i = 0; i < MAX_MOVES; i++)
            {
                GetNextMove(borad);
                Win(borad);
                n++;
            }
            if (n == MAX_MOVES)
            {
                Console.Write("gameover");
            }
            Console.ReadKey();
        }

        static void Win(int[,] borad)
        {
            int i, j, k;
            int key;
            if ((borad[0, 0] == borad[1, 1] && borad[1, 1] == borad[2, 2] && borad[2, 2] == borad[0, 0]) || (borad[2, 0] == borad[1, 1] && borad[1, 1] == borad[0, 2] && borad[2, 0] == borad[0, 2]))
            {
                key = borad[0, 0];
                Whowin(key);
            }
            else
            {
                for (i = 0; i < 3; i++)
                {
                    for (j = 0; j < 3; j++)
                    {
                        if (borad[i, j] == 15 || borad[i, j] == 'X')
                        {
                            key = borad[i, j];
                            for (k = 0; k < 3; k++)
                            {
                                if (borad[i, k] != key)
                                {
                                    break;
                                }
                            }
                            if (k == 3) Whowin(key);
                            for (k = 0; k < 3; k++)
                            {
                                if (borad[k, j] != key)
                                {
                                    break;
                                }
                            }
                            if (k == 3) Whowin(key);
                        }
                    }
                }
            }
        }

        static void Whowin(int key)
        {
            if (key == 15)
            {
                Console.WriteLine("P1 Win!");
            }
            else Console.WriteLine("P2 Win!");
        }

        static void PrintBoard(int[,] borad)
        {
            Console.WriteLine("{0}|{1}|{2}", borad[0, 0], borad[0, 1], borad[0, 2]);
            Console.WriteLine("--+--+--");
            Console.WriteLine("{0}|{1}|{2}", borad[1, 0], borad[1, 1], borad[1, 2]);
            Console.WriteLine("--+--+--");
            Console.WriteLine("{0}|{1}|{2}", borad[2, 0], borad[2, 1], borad[2, 2]);
        }


        static void GetNextMove(int[,] borad)
        {
            int i, j, s, t, m;
            while (true)
            {
                s = -1;
                t = -1;
                Console.Write("Make your move:");
                m = Convert.ToInt32(Console.ReadLine());
                for (i = 0; i < 3; i++)
                {
                    for (j = 0; j < 3; j++)
                    {
                        if (borad[i, j] == m)
                        {
                            s = i;
                            t = j;
                        }
                    }
                }
                if (s == -1 && t == -1)
                {
                    Console.WriteLine("that point already has a pawn!");
                }
                else break;
            }
            if (foot % 2 == 0)
            {
                borad[s, t] = 15;
            }
            else borad[s, t] = 'X';
            foot++;
            Console.Clear();
            PrintBoard(borad);
        }

    }
}

